package com.jec;

import java.util.Properties;

import javax.servlet.Filter;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.vendor.HibernateJpaSessionFactoryBean;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

@SpringBootApplication
//@EnableTransactionManagement(proxyTargetClass=true)
@EnableJpaRepositories
@EnableScheduling
public class TcsApplication {

	public static void main(String[] args) {
		
		Properties properties = Bootstrap.bootstrap();
		
		SpringApplication app = new SpringApplication(TcsApplication.class);
		app.setDefaultProperties(properties);
		app.addListeners(new WebServicePublisher());
		app.run(args);
		
		//SpringApplication.run(TcsApplication.class, args);
	}
	
	

	@Order(Ordered.HIGHEST_PRECEDENCE)  
	@Bean  
	Filter characterEncodingFilter() {  
		CharacterEncodingFilter filter = new CharacterEncodingFilter();  
		filter.setEncoding("UTF-8");  
		filter.setForceEncoding(true);  
		return filter; 
	}

	/*
    @Bean
    public ServletListenerRegistrationBean<EventListener> servletListenerRegistrationBean(){
        ServletListenerRegistrationBean<EventListener> servletListenerRegistrationBean = new ServletListenerRegistrationBean<EventListener>();
        servletListenerRegistrationBean.setListener(new ContextLoaderListener());
        return servletListenerRegistrationBean;
    }
	 */
	
	/*
	@Bean(name = DispatcherServletAutoConfiguration.DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)
	public DispatcherServlet dispatcherServlet()
	{
		return new DispatcherServlet();
	}

	@Bean
	public ServletRegistrationBean dispatcherRegistration() {
		ServletRegistrationBean registration = new ServletRegistrationBean(dispatcherServlet());
		registration.getUrlMappings().clear();
        registration.addUrlMappings("*.html");
        registration.addUrlMappings("*.css");
        registration.addUrlMappings("*.js");
        registration.addUrlMappings("*.png");
        registration.addUrlMappings("*.json");
        registration.addUrlMappings("*.ico");
        registration.addUrlMappings("*.ttf");
        registration.addUrlMappings("*.eot");
        registration.addUrlMappings("*.svg");
        registration.addUrlMappings("*.woff2");
        registration.addUrlMappings("*.woff");
		registration.addUrlMappings("/rest/*");
		return registration;
	}
	
	

	@Bean
	public ServletRegistrationBean cxfServlet() {
		return new ServletRegistrationBean(new CXFServlet(), "/NTNMSWebService/*");
	}
	
	@Bean(name = Bus.DEFAULT_BUS_ID)
	public SpringBus springBus() {
		return new SpringBus();
	}
	
	
	@Bean
	public NTNMS_Service ntmsService() {
		return new NTNMS_ServiceImpl();
	}
	
	@Bean
	public Endpoint endpoint() {
		EndpointImpl endpoint = new EndpointImpl(springBus(), ntmsService());
		endpoint.publish("/NTNMS_Service");
		return endpoint;
	}
	*/

	@Bean
	public HibernateJpaSessionFactoryBean sessionFactory() {
		return new HibernateJpaSessionFactoryBean();
	}
	

	@Bean(name = "multipartResolver")
	public MultipartResolver multipartResolver(){
		CommonsMultipartResolver resolver = new CommonsMultipartResolver();
		resolver.setDefaultEncoding("UTF-8");
		resolver.setResolveLazily(true);//resolveLazily属性启用是为了推迟文件解析，以在在UploadAction中捕获文件大小异常
		resolver.setMaxInMemorySize(40960);
		resolver.setMaxUploadSize(50*1024*1024);//上传文件大小 50M 50*1024*1024
		return resolver;
	}   
	
	


}
